package org.lanqiao.netctoss.controller.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.lanqiao.netctoss.entity.Admin;
import org.lanqiao.netctoss.entity.Permiss;
import org.lanqiao.netctoss.service.impl.AdminServiceImpl;

/**
 * Servlet implementation class AddAdminServlet
 */
public class AddAdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddAdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String name = request.getParameter("name");
		String user  = request.getParameter("user");
		String psw1 = request.getParameter("psw1");
		String psw2 = request.getParameter("psw2");
		Blob usering = null;
		String tel= request.getParameter("tel");
		String email = request.getParameter("email");
		String per="";
		String psw="";
		if(psw1.equals(psw2)){
			psw=psw1;
		}
		String[] permiss = request.getParameterValues("permiss");
		List<Permiss> lp = new ArrayList<Permiss>();
		for(String per_no : permiss){
			Permiss permis = new Permiss();
			permis.setPer_no((Integer.parseInt(per_no)));
			lp.add(permis);
		}				
		/*Admin admin =adminBuss.insAdmin(new Admin(name,user,psw,usering,tel,email));
		adminBuss.insertAdper(new Admin(admin.getAdm_no(),lp));	*/
		int a = new AdminServiceImpl().insertAdmin(new Admin(name,user,psw,usering,tel,email,lp));
		PrintWriter out = response.getWriter();
		boolean flag=false;
		if(a>0){
			flag=true;
			out.println(flag);
			//out.print("<script>alert('恭喜您，添加成功!');window.location.href='../view/admin/admin_add.jsp'</script>");
		}else{
			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
